package com.cdy.jxc_project.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdy.jxc_project.constent.ImportConstent;
import com.cdy.jxc_project.domain.ExportGoods;
import com.cdy.jxc_project.domain.TGoods;
import com.cdy.jxc_project.domain.vo.InventoryQuantityVo;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.mapper.TGoodsMapper;
import com.cdy.jxc_project.service.TGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/23 10:41
 * */
@Service
@Transactional
public class TGoodsServiceImpl extends ServiceImpl<TGoodsMapper, TGoods> implements TGoodsService {

    @Resource
    private TGoodsMapper tGoodsMapper;

    @Override
    public List<TGoods> queryTGoodsList(Integer id) {
        if (id == 0) {
            return tGoodsMapper.selectList(null);
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("type_id", id);
            return tGoodsMapper.selectList(queryWrapper);
        }
    }

    @Override
    public int deleteTGoods(Integer id) {
        return tGoodsMapper.deleteById(id);
    }

    @Override
    public int updateTGoods(TGoods tGoods) {
        return tGoodsMapper.updateById(tGoods);
    }

    @Override
    public int insertTGoods(TGoods tGoods) {
        int insert = tGoodsMapper.insert(tGoods);
        return insert;
    }

    @Override
    public void easyImport(MultipartFile file, R r) {
        String originalFilename = file.getOriginalFilename();
        if (!StringUtils.hasLength(originalFilename)) {
            r.message("导入文件为必传");
            return;
        }
        if (!ImportConstent.ALLOW_FILE_NAME.equals(originalFilename)) {
            r.message("导入文件名必须为" + ImportConstent.ALLOW_FILE_NAME);
            return;
        }

        InputStream in = null;
        try {
            //输入流
            in = file.getInputStream();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setStartSheetIndex(0);
            List<ExportGoods> exportGoods = ExcelImportUtil.importExcel(in, ExportGoods.class, params);
            List<TGoods> goodsList = CollectionUtils.isEmpty(exportGoods) ? new ArrayList<>() : exportGoods.stream().map(entity -> {
                TGoods goods = new TGoods();
                BeanUtils.copyProperties(entity, goods);
                goods.setId(null);
                return goods;
            }).collect(Collectors.toList());
            r = this.saveBatch(goodsList) ? r.message("导入成功") : r.message("导入失败");
            return ;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int updateTGoodsInventoryQuantity(InventoryQuantityVo inventoryQuantityVo) {
        return tGoodsMapper.updateTGoodsInventoryQuantity(inventoryQuantityVo);
    }
}
